package com.example._05mvcboot02.controller;

import com.example._05mvcboot02.mapper.UserMapper;
import com.example._05mvcboot02.pojo.dto.InsertDTO;
import com.example._05mvcboot02.pojo.entity.User;
import com.example._05mvcboot02.pojo.entity.UserVo;
import com.example._05mvcboot02.pojo.vo.UserListVo;
import com.fasterxml.jackson.databind.util.BeanUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 组合注解，相当于Controller + ResponseBody
 * 添加此注解后，所有方法上面不用再添加@ResponseBody注解
 */
//@Controller
@RestController
@RequestMapping("/v1/users/")  //可填写地址共同的与下面进行拼接
public class UserController {
    @Autowired(required = false) //如果为false，装配失败了依旧往下走
    private UserMapper userMapper;

    @PostMapping("insert")
//    @ResponseBody
    public String insert(InsertDTO insertDTO) {
        User user = new User();
        BeanUtils.copyProperties(insertDTO, user);
        user.setCreated(new Date());
        userMapper.insert(user);
        return "添加成功";

    }

    @GetMapping("select")
//    @ResponseBody
    public List<UserListVo> userListVoList() {

        return userMapper.userList();
    }

    /**
     * 删除用户
     */
    @GetMapping("delete")
//    @ResponseBody
    public String userDelete(Long id){
        userMapper.deleteUserId(id);
        return "删除成功";
    }

    @PostMapping("update")
//    @ResponseBody
    public String updateUser(User user){
        userMapper.updateUser(user);
        return "修改成功";
    }


    @GetMapping("selectByid{uid}")
    public UserVo selectById(@PathVariable Long uid){
        return userMapper.selectById(uid);
    }


}





























